package com.xinkao.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.xinkao.example.config.annotation.SysLog;
import com.xinkao.example.service.UserService;
import com.xinkao.example.util.CommonUtil;
import com.xinkao.example.util.RedisUtils;
import com.xinkao.example.util.constants.Constants;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: LXF
 * @description: 用户/角色/权限相关controller
 * @date: 2017/11/2 10:19
 */
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private RedisUtils redisUtils;

	/**
	 * @Author ZSX
	 * @Description 查询用户列表
	 * @Date 2021/01/13 15:30
	 * @Param [requestJson]
	 * @return JSONObject
	 **/
	@SysLog(module = "用户模块", operate = "查询用户列表")
	@RequiresPermissions("user:list")
	@PostMapping("/list")
	public JSONObject listUser(@RequestBody JSONObject requestJson) {

		return userService.listAllUser(requestJson);
	}

	/**
	 * @Author ZSX
	 * @Description 添加用户
	 * @Date 2021/01/13 15:30
	 * @Param [requestJson]
	 * @return JSONObject
	 **/
	@SysLog(module = "用户模块", operate = "添加用户")
	@RequiresPermissions("user:save")
	@PostMapping("/save")
	public JSONObject addUser(HttpServletRequest request,@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "loginName, userName, roleId");
		String token = request.getHeader(Constants.AUTH);
		JSONObject user = redisUtils.get(token, JSONObject.class);
		return userService.addUser(user,requestJson);
	}

	/**
	 * @Author ZSX
	 * @Description 修改用户
	 * @Date 2021/01/13 16:00
	 * @Param [requestJson]
	 * @return JSONObject
	 **/
	@SysLog(module = "用户模块", operate = "修改用户")
	@RequiresPermissions("user:edit")
	@PostMapping("/edit")
	public JSONObject updateUser(HttpServletRequest request,@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "userId");
		String token = request.getHeader(Constants.AUTH);
		JSONObject user = redisUtils.get(token, JSONObject.class);
		return userService.updateUser(user,requestJson);
	}

	/**
	 * @Author ZSX
	 * @Description 重置密码
	 * @Date 2021/01/13 16:00
	 * @Param [request]
	 * @return JSONObject
	 **/
	@SysLog(module = "用户模块", operate = "重置密码")
	@RequiresPermissions("user:resetPassword")
	@PostMapping("/resetPassword")
	public JSONObject resetPassword(@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "userId");
		return userService.resetPassword(requestJson);
	}

	/**
	 * @Author ZSX
	 * @Description 修改密码
	 * @Date 2021/01/13 16:00
	 * @Param [requestJson]
	 * @return JSONObject
	 **/
	@SysLog(module = "用户模块", operate = "修改密码")
	@RequiresPermissions("user:modifyPassword")
	@PostMapping("/modifyPassword")
	public JSONObject updatePassword(HttpServletRequest request,@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "oldPassword,newPassword");
		String token = request.getHeader(Constants.AUTH);
		JSONObject user = redisUtils.get(token, JSONObject.class);
		return userService.updatePassword(user,requestJson);
	}

	/**
	 * @Author ZSX
	 * @Description 删除
	 * @Date 2021/01/13 16:30
	 * @Param [requestJson]
	 * @return JSONObject
	 **/
	@SysLog(module = "用户模块", operate = "删除")
	@RequiresPermissions("user:delete")
	@GetMapping("/delete")
	public JSONObject deleteUser(HttpServletRequest request) {
		CommonUtil.hasAllRequired(CommonUtil.request2Json(request), "userId");
		return userService.deleteUser(CommonUtil.request2Json(request));
	}

}
